﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataAccess;

namespace DomMgmtSystem.View
{
    public partial class MEDIUM_HD : System.Web.UI.Page
    {
        #region 属性
        /// <summary>
        /// 
        /// </summary>
        private decimal TB_HSSJTG_ID
        {
            get
            {
                if (!string.IsNullOrEmpty(Request["hssjtgID"]))
                    return Convert.ToDecimal(Request["hssjtgID"]);
                return 0;
            }
        }
        /// <summary>
        /// 是否是添加
        /// </summary>
        public bool IsNew
        {
            get
            {
                if (ViewState["IsNew"] != null)
                    return Convert.ToBoolean(ViewState["IsNew"]);
                else
                    return this.GetRequestStr("isnew") != string.Empty;
            }

            set
            {
                ViewState["IsNew"] = value;
            }
        }

        /// <summary>
        /// 是否是浏览
        /// </summary>
        public bool IsView
        {
            get
            {
                return this.GetRequestStr("view") != string.Empty;
            }
        }

        #endregion
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                DataBindPage();

            if (Request.Params["view"] != null)
                ClientScript.RegisterStartupScript(typeof(Page), "a", "<script>ChangeToLabel();imghide();</script>");

        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            Model.MEDIUM_HD_T model = null;
            if (TB_HSSJTG_ID != 0)
                model = DataAccess.MEDIUM_HD_T.GetModel(TB_HSSJTG_ID);
            else
                model = new Model.MEDIUM_HD_T();

            model.CREATEDATE = DateTime.Now;
            model.CREATEUSER = Session["userID"].ToString();
            model.MEDIUM_DYZT = Convert.ToDecimal(ddlMEDIUM_DYZT.SelectedValue);
            model.MEDIUM_LY = txtMEDIUM_LY.Text.Trim();
            model.MEDIUM_SN = txtMEDIUM_SN.Text.Trim();
            model.MEDIUM_SYZT = Convert.ToDecimal(ddlMEDIUM_SYZT.SelectedValue);
            model.MEDIUM_YT = ddlMEDIUM_YT.SelectedValue;

            if (model.MEDIUM_SYZT == 1)//已使用
            {
                if (hidJZBH.Value.IsNullOrEmpty())
                {
                    this.Alert("介质编号不能为空，请输入摄区代号查询介质编号！");
                    return;
                }
                model.MEDIUM_BH = hidJZBH.Value;
                model.MEDIUM_DJXH = txtMEDIUM_DJXH.Text.Trim().IsNotEmpty() ? Convert.ToDecimal(txtMEDIUM_DJXH.Text.Trim()) : 0;
                model.MEDIUM_LX = Convert.ToDecimal(ddlMEDIUM_LX.SelectedValue);
                model.MEDIUM_SYDW = txtMEDIUM_SYDW.Text.Trim();
                model.MEDIUM_USERQ = txtMEDIUM_USERQ.Text.Trim().IsNotEmpty() ? Convert.ToDateTime(txtMEDIUM_USERQ.Text.Trim()) : DateTime.MinValue;
                model.MEDIUM_YHTGDW = txtMEDIUM_YHTGDW.Text.Trim();
                model.MEDIUM_YHTGRQ = txtMEDIUM_YHTGRQ.Text.Trim().IsNotEmpty() ? Convert.ToDateTime(txtMEDIUM_YHTGRQ.Text.Trim()) : DateTime.MinValue;
                model.MEDIUM_ZLLX = ddlMEDIUM_ZLLX.SelectedValue;
                model.MEDIUM_ZLMC = txtMEDIUM_ZLMC.Text.Trim();
            }
            else
            {
                model.MEDIUM_BH = CommonExecute.GetBarCode("1", "0");
            }

            if (TB_HSSJTG_ID != 0)
            {
                if (DataAccess.MEDIUM_HD_T.Update(model))
                    this.RedirectAfterInfo("编辑成功！", "MEDIUM_HD_List.aspx");
                else
                    this.Alert("编辑失败！");

            }
            else
            {
                if (DataAccess.MEDIUM_HD_T.Add(model))

                    this.RedirectAfterInfo("增加成功！", "MEDIUM_HD_List.aspx");
                else
                    this.Alert("增加失败！");
            }
        }

        private void DataBindPage()
        {
            lbTitle.Text = "新增信息";
            if (TB_HSSJTG_ID != 0)
            {
                btnBack.Visible = true;

                if (IsView)
                    lbTitle.Text = "查看信息";
                else
                    lbTitle.Text = "编辑信息";
                Model.MEDIUM_HD_T model = DataAccess.MEDIUM_HD_T.GetModel(TB_HSSJTG_ID);
                if (model.MEDIUM_SYZT == 0)
                {
                    txtMEDIUM_SN.Text = model.MEDIUM_SN;

                    txtMEDIUM_LY.Text = model.MEDIUM_LY;
                    ddlMEDIUM_DYZT.Items.FindByValue(model.MEDIUM_DYZT.ToString()).Selected = true;
                    ddlMEDIUM_SYZT.Items.FindByValue(model.MEDIUM_SYZT.ToString()).Selected = true;
                    ddlMEDIUM_YT.Items.FindByValue(model.MEDIUM_YT.ToString()).Selected = true;
                    ddlMEDIUM_JZBH.Items.Add(model.MEDIUM_BH);
                    return;
                }
                //ddlMEDIUM_JZBH.Items.FindByValue(model.MEDIUM_BH).Selected = true;
                ddlMEDIUM_JZBH.Items.Add(model.MEDIUM_BH);
                hidJZBH.Value = model.MEDIUM_BH;
                txtMEDIUM_DJXH.Text = model.MEDIUM_DJXH.ToString();
                txtMEDIUM_SYDW.Text = model.MEDIUM_SYDW;
                txtMEDIUM_USERQ.Text = model.MEDIUM_USERQ.Value.ToString("yyyy-MM-dd");
                txtMEDIUM_YHTGDW.Text = model.MEDIUM_YHTGDW;
                txtMEDIUM_YHTGRQ.Text = model.MEDIUM_YHTGRQ.Value.ToString("yyyy-MM-dd");
                txtMEDIUM_ZLMC.Text = model.MEDIUM_ZLMC;
                ddlMEDIUM_LX.Items.FindByValue(model.MEDIUM_LX.ToString()).Selected = true;
                ddlMEDIUM_ZLLX.Items.FindByValue(model.MEDIUM_ZLLX.ToString()).Selected = true;

            }
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("MEDIUM_HD_List.aspx");
        }

        protected void ddlMEDIUM_JZBH_SelectedIndexChanged(object sender, EventArgs e)
        {
            Model.MEDIUM_HD_T model = DataAccess.MEDIUM_HD_T.GetModelBYBH(ddlMEDIUM_JZBH.SelectedValue);
            if (model != null)
            {
                ddlMEDIUM_JZBH.Items.FindByValue(model.MEDIUM_BH).Selected = true;
                txtMEDIUM_SN.Text = model.MEDIUM_SN;
                txtMEDIUM_DJXH.Text = model.MEDIUM_DJXH.ToString();
                txtMEDIUM_LY.Text = model.MEDIUM_LY;
                txtMEDIUM_SN.Text = model.MEDIUM_SN;
                txtMEDIUM_SYDW.Text = model.MEDIUM_SYDW;
                txtMEDIUM_USERQ.Text = model.MEDIUM_USERQ.Value.ToString("yyyy-MM-dd");
                txtMEDIUM_YHTGDW.Text = model.MEDIUM_YHTGDW;
                txtMEDIUM_YHTGRQ.Text = model.MEDIUM_YHTGRQ.Value.ToString("yyyy-MM-dd");
                txtMEDIUM_ZLMC.Text = model.MEDIUM_ZLMC;
                ddlMEDIUM_DYZT.Items.FindByValue(model.MEDIUM_DYZT.ToString()).Selected = true;
                ddlMEDIUM_LX.Items.FindByValue(model.MEDIUM_LX.ToString()).Selected = true;
                ddlMEDIUM_SYZT.Items.FindByValue(model.MEDIUM_SYZT.ToString()).Selected = true;
                ddlMEDIUM_YT.Items.FindByValue(model.MEDIUM_YT.ToString()).Selected = true;
                ddlMEDIUM_ZLLX.Items.FindByValue(model.MEDIUM_ZLLX.ToString()).Selected = true;
            }
        }
    }
}